home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / page-ext.el < prev    next >
Text File  |  1993-07-23  |  29KB  |  769 lines

  1. ;;; page-ext.el --- page handling commands
  2.  
  3. ;;; Copyright (C) 1990 Free Software Foundation
  4.  
  5. ;; Author: Robert J. Chassell <bob@gnu.ai.mit.edu>
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;;; You may use these commands to handle an address list or other
  26. ;;; small data base.
  27.  
  28. ;;; Change Log:
  29.  
  30. ;;; Change Log ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  31. ;;;
  32. ;;; Version 0.043
  33. ;;; 24 May 1990 - When the cursor is at the end of the pages directory
  34. ;;; buffer (which is empty), a `C-c C-c' (pages-directory-goto)
  35. ;;; command now takes you to the end of the buffer.
  36. ;;;
  37. ;;; Version 0.042
  38. ;;; 16 May 1990 - Since people often handle address and other files
  39. ;;; differently, variable `pages-directory-for-addresses-narrowing-p'
  40. ;;; now specifies whether `pages-directory-goto' should narrow
  41. ;;; addresses buffer to entry to which it goes.
  42. ;;; `pages-directory-buffer-narrowing-p' continues to control
  43. ;;; narrowing of pages buffer.
  44. ;;;
  45. ;;; `add-new-page' documentation string now explains
  46. ;;; that the value of the inserted page-delimiter is a `^L'.
  47. ;;;
  48. ;;; `pages-directory-previous-regexp' definition reworded.
  49. ;;;
  50. ;;; Removed unneeded defvar for `pages-directory-buffer'.
  51. ;;;
  52. ;;; Version 0.041 
  53. ;;; 14 May 1990 - `pages-last-search' bound to nil initially.
  54. ;;; Remove unnecessary lines from `search-pages' definition.
  55. ;;;
  56. ;;; Version 0.04 
  57. ;;; 18 Mar 1990 - `pages-directory' creates a directory for only the
  58. ;;; accessible portion of the buffer; it does not automatically widen
  59. ;;; the buffer.
  60. ;;;  
  61. ;;; However, `pages-directory-for-addresses' does widen the addresses'
  62. ;;; buffer before constructing the addresses' directory.
  63. ;;;
  64. ;;; Version 0.032
  65. ;;; 20 Feb 1990 - `pages-directory-for-addresses' no longer copies
  66. ;;; first line of addresses directory to kill-ring
  67. ;;;
  68. ;;; Remove `(kill-all-local-variables)' line from
  69. ;;; `pages-directory-address-mode' so Emacs will not be told to forget
  70. ;;; the name of the file containing the addresses!
  71. ;;;
  72. ;;; Version 0.031
  73. ;;; 15 Feb 1990 - `pages-directory-goto' no longer erroneously selects
  74. ;;; the entry on the following line when the cursor is at the end of
  75. ;;; the line, but selects the entry on which the cursor rests.  
  76. ;;;
  77. ;;; `pages-directory-address-mode' now sets local variables and enables
  78. ;;; `describe-mode' to describe Addresses Directory mode.
  79. ;;;
  80. ;;; `pages-directory-for-addresses' now sets the buffer-modified flag
  81. ;;; for the Addresses Directory to nil.
  82. ;;;
  83. ;;; The documentation string for both `pages-directory-mode' and
  84. ;;; `pages-directory-address-mode' now provide a lookup for the
  85. ;;; `pages-directory-goto' keybinding.
  86. ;;;
  87. ;;; Version 0.03
  88. ;;; 10 Feb 1990 - Incorporated a specialized extension of the
  89. ;;; `pages-directory' command called `pages-directory-for-addresses'
  90. ;;; and bound it to ctl-x-ctl-p-map "d" for integration with other
  91. ;;; page functions.  This function finds a file, creates a directory
  92. ;;; for it using the `pages-directory' command, and displays the
  93. ;;; directory.  It is primarily for lists of addresses and the like.
  94. ;;;
  95. ;;; The difference between this and the `pages-directory' command is
  96. ;;; that the `pages-directory-for-addresses' command presumes a
  97. ;;; default addresses file (although you may optionally specify a file
  98. ;;; name) and it switches you to the directory for the file, but the
  99. ;;; `pages-directory' command creates a directory for the current
  100. ;;; buffer, and pops to the directory in another window.
  101. ;;;
  102. ;;; `pages-directory' now places the cursor over the header line of
  103. ;;; the page in which point was located in the pages buffer.
  104. ;;;
  105. ;;; New `set-page-delimiter' command sets  the buffer local value of
  106. ;;; the page-delimiter variable.  With prefix arg, resets function to
  107. ;;; original value.  (Quicker to use than `edit-options'.)
  108. ;;;
  109. ;;; Version 0.02
  110. ;;; 9 Feb 1990 - `pages-directory' now displays the
  111. ;;; first line that contains a non-blank character that follows the
  112. ;;; `page-delimiter'; this may be the rest of the line that contains
  113. ;;; the `page-delimiter' or a line following.  (In most instances, the
  114. ;;; line containing a non-blank character is a line of text.)
  115. ;;; Modification includes changes to `pages-copy-header-and-position'.
  116. ;;;
  117. ;;; Each directory created by `pages-directory' now possesses a name
  118. ;;; derived on the name of the pages buffer.  Consequently, you may
  119. ;;; create several different directories, one for each pages buffer.
  120. ;;;
  121. ;;; `sort-pages-in-region' no longers requires the text to start on
  122. ;;; the line immediately following the line containing the
  123. ;;; page-delimiter.
  124. ;;;
  125. ;;; `pages-directory-goto' no longer narrows to the page
  126. ;;; automatically.  Instead, if you wish it to narrow to the page, set
  127. ;;; variable pages-directory-buffer-narrowing-p to a non-nil value.
  128. ;;; Default is nil; this is an experiment to see whether it is useful
  129. ;;; to see the surrounding context.
  130. ;;;
  131. ;;; Version 0.011
  132. ;;; 2 Feb 1990 - `add-new-page': removed extraneous space.
  133. ;;;
  134. ;;; Version 0.01 
  135. ;;; 28 Jan 1990 - Initial definitions.
  136. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  137.  
  138. ;;; Code:
  139.  
  140. ;;;; Summary
  141.  
  142. ; The current page commands are:
  143.  
  144. ;     forward-page         C-x ]
  145. ;     backward-page        C-x [
  146. ;     narrow-to-page       C-x p
  147. ;     count-lines-page     C-x l
  148. ;     mark-page            C-x C-p  (change this to C-x C-p C-m)
  149. ;     sort-pages           not bound
  150. ;     what-page            not bound
  151.  
  152. ; The new page handling commands all use `C-x C-p' as a prefix.  This
  153. ; means that the key binding for `mark-page' must be changed.
  154. ; Otherwise, no other changes are made to the current commands or
  155. ; their bindings.
  156.  
  157. ; New page handling commands:
  158.  
  159. ;     next-page                        C-x C-p C-n
  160. ;     previous-page                    C-x C-p C-p
  161. ;     search-pages                     C-x C-p C-s
  162. ;     add-new-page                     C-x C-p C-a
  163. ;     sort-pages-buffer                C-x C-p s      
  164. ;     set-page-delimiter               C-x C-p C-l
  165. ;     pages-directory                  C-x C-p C-d   
  166. ;     pages-directory-for-addresses    C-x C-p d
  167. ;         goto-page                    C-c C-c
  168.  
  169.  
  170. ;;;; Using the page commands
  171. ; The page commands are helpful in several different contexts.  For
  172. ; example, programmers often divide source files into sections using the
  173. ; `page-delimiter'; you can use the `pages-directory' command to list
  174. ; the sections.
  175.  
  176. ; You may change the buffer local value of the `page-delimiter' with
  177. ; the `set-page-delimiter' command.  This command is bound to `C-x C-p
  178. ; C-l' The command prompts you for a new value for the page-delimiter.
  179. ; Called with a prefix-arg, the command resets the value of the
  180. ; page-delimiter to its original value.
  181.  
  182.  
  183. ;;;; Handling an address list or small data base
  184.  
  185. ; You may use the page commands to handle an address list or other
  186. ; small data base.  Put each address or entry on its own page.  The
  187. ; first line of text in each page is a `header line' and is listed by
  188. ; the `pages-directory' or `pages-directory-for-addresses' command.
  189.  
  190. ; Specifically:
  191. ;   1. Begin each entry with a `page-delimiter' (which is, by default,
  192. ;      `^L' at the beginning of the line).
  193. ;   2. The first line of text in each entry is the `heading line'; it
  194. ;      will appear in the pages-directory-buffer which is constructed
  195. ;      using the `C-x C-p C-d' (pages-directory) command or the `C-x
  196. ;      C-p d' (pages-directory-for-addresses) command.
  197. ;      The heading line may be on the same line as the page-delimiter
  198. ;      or it may follow after.  It is the first non-blank line on the
  199. ;      page.  Conventionally, the heading line is placed on the line
  200. ;      immediately following the line containing page-delimiter.
  201. ;
  202. ;   3. Follow the heading line with the body of the entry.  The body
  203. ;      extends up to the next `page-delimiter'.  The body may be of any
  204. ;      length.  It is conventional to place a blank line after the last
  205. ;      line of the body.
  206.  
  207. ; For example, a file might look like this:
  208. ;     FSF
  209. ;     Free Software Foundation
  210. ;     675 Massachusetts Avenue
  211. ;     Cambridge, MA 02139  USA
  212. ;     (617) 876-3296
  213. ;     gnu@prep.ai.mit.edu
  214. ;     
  215. ;     House Subcommittee on Intellectual Property,
  216. ;     U.S. House of Representatives,
  217. ;     Washington, DC  20515
  218. ;     
  219. ;     Congressional committee concerned with permitting or preventing
  220. ;     monopolistic restrictions on the use of software technology
  221. ;     
  222. ;     George Lakoff
  223. ;     ``Women, Fire, and Dangerous Things:
  224. ;     What Categories Reveal about the Mind''
  225. ;     1987, Univ. of Chicago Press
  226. ;     About philosophy, Whorfian effects, and linguistics.
  227. ;       
  228. ;     OBI   (On line text collection.)
  229. ;     Open Book Initiative
  230. ;     c/o Software Tool & Die
  231. ;     1330 Beacon St, Brookline, MA 02146 USA
  232. ;     (617) 739-0202 
  233. ;     obi@world.std.com
  234.  
  235. ; In this example, the heading lines are:
  236. ;
  237. ;     FSF
  238. ;     House Subcommittee on Intellectual Property
  239. ;     George Lakoff
  240. ;     OBI (On line text collection.)
  241.  
  242. ; The `C-x C-p s' (sort-pages-buffer) command sorts the entries in the
  243. ; buffer alphabetically.
  244.  
  245. ; You may use any of the page commands, including the `next-page',
  246. ; `previous-page', `add-new-page', `mark-page', and `search-pages'
  247. ; commands.
  248.  
  249. ; You may use either the `C-x C-p d' (pages-directory-for-addresses)
  250. ; or the `C-x C-p C-d' (pages-directory) command to construct and
  251. ; dislay a directory of all the heading lines.
  252.  
  253. ; In the directory, you may position the cursor over a heading line
  254. ; and type `C-c C-c' to go to the entry to which it refers in the
  255. ; pages buffer.
  256.  
  257. ; When used in conjunction with the `pages-directory-for-addresses'
  258. ; command, the `C-c C-c' (pages-directory-goto) command narrows to the
  259. ; entry to which it goes.  But, when used in conjunction with the
  260. ; `pages-directory' command, the `C-c C-c' (pages-directory-goto)
  261. ; command does not narrow to the entry, but widens the buffer so you
  262. ; can see the context surrounding the entry.
  263.  
  264. ; If you wish, you may create several different directories,
  265. ; one for each different buffer.
  266.  
  267. ;; `pages-directory-for-addresses' in detail
  268.  
  269. ; The `pages-directory-for-addresses' assumes a default addresses
  270. ; file.  You do not need to specify the addresses file but merely type
  271. ; `C-x C-p d' from any buffer.  The command finds the file, constructs
  272. ; a directory for it, and switches you to the directory.  If you call
  273. ; the command with a prefix arg, `C-u C-x C-p d', it prompts you for a
  274. ; file name.
  275.  
  276. ;; `pages-directory' in detail
  277.  
  278. ; Call the `pages-directory' from the buffer for which you want a
  279. ; directory created; it creates a directory for the buffer and pops
  280. ; you to the directory.
  281.  
  282. ; The `pages-directory' command has several options:
  283.  
  284. ;   Called with a prefix arg, `C-u C-x C-p C-d', the `pages-directory'
  285. ;   prompts you for a regular expression and only lists only those
  286. ;   header lines that are part of pages that contain matches to the
  287. ;   regexp.  In the example above, `C-u C-x C-p C-d 617 RET' would
  288. ;   match the telephone area code of the first and fourth entries, so
  289. ;   only the header lines of those two entries would appear in the
  290. ;   pages-directory-buffer.
  291. ;   Called with a numeric argument, the `pages-directory' command
  292. ;   lists the number of lines in each page.  This is helpful when you
  293. ;   are printing hardcopy.  
  294.  
  295. ;   Called with a negative numeric argument, the `pages-directory'
  296. ;   command lists the lengths of pages whose contents match a regexp.
  297.  
  298.  
  299. ;;;; Key bindings for page handling functions
  300.  
  301. (global-unset-key "\C-x\C-p")
  302.  
  303. (defvar ctl-x-ctl-p-map (make-sparse-keymap)
  304.   "Keymap for subcommands of C-x C-p, which are for page handling.")
  305.  
  306. (define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix)
  307. (fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map)
  308.  
  309. (define-key ctl-x-ctl-p-map "\C-n" 'next-page)
  310. (define-key ctl-x-ctl-p-map "\C-p" 'previous-page)
  311. (define-key ctl-x-ctl-p-map "\C-a" 'add-new-page)
  312. (define-key ctl-x-ctl-p-map "\C-m" 'mark-page)
  313. (define-key ctl-x-ctl-p-map "\C-s" 'search-pages)
  314. (define-key ctl-x-ctl-p-map "s"    'sort-pages-buffer)
  315. (define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter)
  316. (define-key ctl-x-ctl-p-map "\C-d" 'pages-directory)
  317. (define-key ctl-x-ctl-p-map "d"    'pages-directory-for-addresses)
  318.  
  319.  
  320. ;;;; Page movement function definitions
  321.  
  322. (defun next-page (&optional count)
  323.   "Move to the next page bounded by the `page-delimiter' variable.
  324. With arg (prefix if interactive), move that many pages."
  325.   (interactive "p")
  326.   (or count (setq count 1))
  327.   (widen)
  328.   ;; Cannot use forward-page because of problems at page boundaries.
  329.   (while (and (> count 0) (not (eobp)))
  330.     (if (re-search-forward page-delimiter nil t)
  331.         nil
  332.       (goto-char (point-max)))
  333.     (setq count (1- count)))
  334.   ;; If COUNT is negative, we want to go back -COUNT + 1 page boundaries.
  335.   ;; The first page boundary we reach is the top of the current page,
  336.   ;; which doesn't count.
  337.   (while (and (< count 1) (not (bobp)))
  338.     (if (re-search-backward page-delimiter nil t)
  339.     (goto-char (match-beginning 0))
  340.       (goto-char (point-min)))
  341.     (setq count (1+ count)))
  342.   (narrow-to-page)
  343.   (goto-char (point-min))
  344.   (recenter 0))
  345.  
  346. (defun previous-page (&optional count)
  347.   "Move to the previous page bounded by the `page-delimiter' variable.
  348. With arg (prefix if interactive), move that many pages."
  349.   (interactive "p")
  350.   (or count (setq count 1))
  351.   (next-page (- count)))
  352.  
  353.  
  354. ;;;; Adding and searching pages
  355.  
  356. (defun add-new-page (header-line)
  357.   "Insert new page at point; prompt for header line.
  358. Page begins with a `^L' as the page-delimiter.  
  359. Point is left in the body of page."
  360.   (interactive "sHeader line: ")
  361.   (widen)
  362.   (insert (format "\n \n%s\n\n" header-line))
  363.   ;; don't renarrow; stay unnarrowed to see context
  364.   (forward-line -1))
  365.  
  366. (defvar pages-last-search nil
  367.   "Value of last regexp searched for.  Initially, nil.")
  368.  
  369. (defun search-pages (regexp)
  370.   "Search for REGEXP, starting from point, and narrow to page it is in."
  371.   (interactive (list
  372.                 (read-string
  373.                  (format "Search for `%s' (end with RET): "
  374.                          (or pages-last-search "regexp")))))
  375.   (if (equal regexp "")
  376.       (setq regexp pages-last-search)
  377.     (setq pages-last-search regexp))
  378.   (widen)
  379.   (re-search-forward regexp)
  380.   (narrow-to-page))
  381.  
  382.  
  383. ;;;; Sorting pages
  384.  
  385. (autoload 'sort-subr "sort" "Primary function for sorting." t nil)
  386.  
  387. (defun sort-pages-in-region (reverse beg end)
  388.   "Sort pages in region alphabetically.  Prefix arg means reverse order.  
  389.  
  390. Called from a program, there are three arguments:
  391. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  392.  
  393. ;;; This sort function handles ends of pages differently than
  394. ;;; `sort-pages' and works better with lists of addresses and similar
  395. ;;; files.
  396.  
  397.   (interactive "P\nr")
  398.   (save-restriction
  399.     (narrow-to-region beg end)
  400.     (goto-char (point-min))
  401.     ;;; `sort-subr' takes three arguments
  402.     (sort-subr reverse
  403.  
  404.                ;; NEXTRECFUN is called with point at the end of the
  405.                ;; previous record. It moves point to the start of the
  406.                ;; next record.
  407.            (function (lambda ()
  408.                            (re-search-forward page-delimiter nil t)
  409.                            (skip-chars-forward " \t\n")
  410.                            ))
  411.  
  412.                ;; ENDRECFUN is is called with point within the record.
  413.                ;; It should move point to the end of the record.
  414.            (function (lambda ()
  415.                            (if (re-search-forward
  416.                                 page-delimiter
  417.                                 nil
  418.                                 t)
  419.                                (goto-char (match-beginning 0))
  420.                              (goto-char (point-max))))))))
  421.  
  422. (defun sort-pages-buffer (&optional reverse)
  423.   "Sort pages alphabetically in buffer.  Prefix arg means reverse order. 
  424. \(Non-nil arg if not interactive.\)"
  425.  
  426.   (interactive "P")
  427.   (or reverse (setq reverse nil))
  428.   (widen)
  429.   (let ((beginning (point-min))
  430.         (end (point-max)))
  431.     (sort-pages-in-region reverse beginning end)))
  432.  
  433.  
  434. ;;;; Pages directory ancillary definitions
  435.  
  436. (defvar pages-directory-buffer-narrowing-p nil
  437.   "*If non-nil, `pages-directory-goto' narrows pages buffer to entry.")
  438.  
  439. (defvar pages-directory-previous-regexp nil
  440.   "Value of previous regexp used by `pages-directory'.
  441. \(This regular expression may be used to select only those pages that
  442. contain matches to the regexp.\)")
  443.  
  444. (defvar pages-buffer nil
  445.   "The buffer for which the pages-directory function creates the directory.")
  446.  
  447. (defvar pages-directory-prefix "*Directory for:"
  448.   "Prefix of name of temporary buffer for pages-directory.")
  449.  
  450. (defvar pages-pos-list nil
  451.   "List containing the positions of the pages in the pages-buffer.")
  452.  
  453. (defvar pages-directory-map nil
  454.   "Keymap for the pages-directory-buffer.")
  455.  
  456. (if pages-directory-map
  457.     ()
  458.   (setq pages-directory-map (make-sparse-keymap))
  459.   (define-key pages-directory-map "\C-c\C-c"
  460.     'pages-directory-goto))
  461.  
  462. (defun set-page-delimiter (regexp reset-p)
  463.   "Set buffer local value of page-delimiter to REGEXP.
  464. Called interactively with a prefix argument, reset `page-delimiter' to
  465. its original value.
  466.  
  467. In a program, non-nil second arg causes first arg to be ignored and
  468. resets the page-delimiter to the original value."
  469.   
  470.   (interactive
  471.    (if current-prefix-arg
  472.        (list original-page-delimiter nil)
  473.      (list (read-string "Set page-delimiter to regexp: " page-delimiter)
  474.            nil)))
  475.   (make-local-variable 'original-page-delimiter)
  476.   (make-local-variable 'page-delimiter)
  477.   (setq original-page-delimiter
  478.         (or original-page-delimiter page-delimiter))
  479.   (if (not reset-p)
  480.       (setq page-delimiter regexp)
  481.     (setq page-delimiter original-page-delimiter))
  482.   (if (interactive-p)
  483.       (message "The value of `page-delimiter' is now: %s" page-delimiter)))
  484.  
  485.  
  486. ;;;; Pages directory main definitions
  487.  
  488. (defun pages-directory
  489.   (pages-list-all-headers-p count-lines-p &optional regexp)
  490.   "Display a directory of the page headers in a temporary buffer.
  491. A header is the first non-blank line after the page-delimiter.
  492. \\[pages-directory-mode]
  493. You may move point to one of the lines in the temporary buffer,
  494. then use \\<pages-directory-goto> to go to the same line in the pages buffer.
  495.  
  496. In interactive use:
  497.  
  498.     1. With no prefix arg, display all headers.
  499.  
  500.     2. With prefix arg, display the headers of only those pages that
  501.        contain matches to a regular expression for which you are
  502.        prompted.
  503.  
  504.     3. With numeric prefix arg, for every page, print the number of
  505.        lines within each page.
  506.  
  507.     4. With negative numeric prefix arg, for only those pages that
  508.        match a regular expression, print the number of lines within
  509.        each page.
  510.  
  511. When called from a program, non-nil first arg means list all headers;
  512. non-nil second arg means print numbers of lines in each page; if first
  513. arg is nil, optional third arg is regular expression.
  514.  
  515. If the buffer is narrowed, the `pages-directory' command creates a
  516. directory for only the accessible portion of the buffer."
  517.   
  518.   (interactive
  519.    (cond ((not current-prefix-arg)
  520.           (list t nil nil))
  521.          ((listp current-prefix-arg) 
  522.           (list nil
  523.                 nil
  524.                 (read-string
  525.                  (format "Select according to `%s' (end with RET): "
  526.                          (or pages-directory-previous-regexp "regexp")))))
  527.          ((> (prefix-numeric-value current-prefix-arg) 0)
  528.           (list t t nil))
  529.          ((< (prefix-numeric-value current-prefix-arg) 0)          
  530.           (list nil
  531.                 t
  532.                 (read-string
  533.                  (format "Select according to `%s' (end with RET): "
  534.                          (or pages-directory-previous-regexp "regexp")))))))
  535.   
  536.   (if (equal regexp "")
  537.       (setq regexp pages-directory-previous-regexp)
  538.     (setq pages-directory-previous-regexp regexp))
  539.   
  540.   (if (interactive-p)
  541.       (message "Creating directory for: %s "
  542.                (buffer-name)))
  543.   
  544.   (let ((buffer (current-buffer))
  545.         (pages-directory-buffer
  546.          (concat pages-directory-prefix " " (buffer-name) " "))
  547.         (linenum 1)
  548.         (pages-buffer-original-position (point))
  549.         (pages-buffer-original-page 0))
  550.     
  551.     ;; `with-output-to-temp-buffer' binds the value of the variable
  552.     ;; `standard-output' to the buffer named as its first argument,
  553.     ;; but does not switch to that buffer.
  554.     (with-output-to-temp-buffer pages-directory-buffer
  555.       (save-excursion
  556.         (set-buffer standard-output)
  557.         (pages-directory-mode)
  558.         (insert
  559.          "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
  560.         (setq pages-buffer buffer)
  561.         (setq pages-pos-list nil))
  562.       
  563.       (if pages-list-all-headers-p
  564.           
  565.           ;; 1. If no prefix argument, list all headers
  566.           (save-excursion
  567.             (goto-char (point-min))
  568.             
  569.             ;; (a) Point is at beginning of buffer; but the first
  570.             ;;     page may not begin with a page-delimiter
  571.             (save-restriction
  572.               ;; If page delimiter is at beginning of buffer, skip it
  573.               (if (and (save-excursion
  574.                          (re-search-forward page-delimiter nil t))
  575.                        (= 1 (match-beginning 0)))
  576.                   (goto-char (match-end 0)))
  577.               (narrow-to-page)
  578.               (pages-copy-header-and-position count-lines-p))
  579.             
  580.             ;; (b) Search within pages buffer for next page-delimiter
  581.             (while (re-search-forward page-delimiter nil t)
  582.               (pages-copy-header-and-position count-lines-p)))
  583.         
  584.         ;; 2. Else list headers whose pages match regexp.
  585.         (save-excursion
  586.           ;; REMOVED  save-restriction  AND  widen  FROM HERE
  587.           (goto-char (point-min))
  588.           
  589.           ;; (a) Handle first page
  590.           (save-restriction
  591.             (narrow-to-page)
  592.             ;; search for selection regexp
  593.             (if (save-excursion (re-search-forward regexp nil t))
  594.                 (pages-copy-header-and-position count-lines-p)))
  595.           
  596.           ;; (b) Search for next page-delimiter
  597.           (while (re-search-forward page-delimiter nil t)
  598.             (save-restriction
  599.               (narrow-to-page)
  600.               ;; search for selection regexp
  601.               (if (save-excursion (re-search-forward regexp nil t))
  602.                   (pages-copy-header-and-position count-lines-p)
  603.                 )))))
  604.       
  605.       (set-buffer standard-output)
  606.       ;; Put positions in increasing order to go with buffer.
  607.       (setq pages-pos-list (nreverse pages-pos-list))
  608.       (if (interactive-p)
  609.           (message "%d matching lines in: %s"
  610.                    (length pages-pos-list) (buffer-name buffer))))
  611.     (pop-to-buffer pages-directory-buffer)
  612.     (sit-for 0)  ; otherwise forward-line fails if N > window height.
  613.     (forward-line (if (= 0 pages-buffer-original-page)
  614.                       1
  615.                     pages-buffer-original-page))))
  616.  
  617. (defun pages-copy-header-and-position (count-lines-p)
  618.   "Copy page header and its position to the Pages Directory.
  619. Only arg non-nil, count lines in page and insert before header.
  620. Used by `pages-directory' function."
  621.   
  622.   (let (position line-count)
  623.  
  624.     (if count-lines-p
  625.         (save-excursion
  626.           (save-restriction
  627.             (narrow-to-page)
  628.             (setq line-count (count-lines (point-min) (point-max))))))
  629.  
  630.     ;; Keep track of page for later cursor positioning
  631.     (if (<= (point) pages-buffer-original-position)
  632.         (setq pages-buffer-original-page
  633.               (1+ pages-buffer-original-page)))
  634.     
  635.     (save-excursion
  636.       ;; go to first non-blank char after the page-delimiter
  637.       (skip-chars-forward " \t\n")     
  638.       ;; set the marker here; this the place to which the
  639.       ;; `pages-directory-goto' command will go 
  640.       (setq position (make-marker))
  641.       (set-marker position (point))
  642.       (let ((start (point))
  643.             (end (save-excursion (end-of-line) (point))))
  644.         ;; change to directory buffer
  645.         (set-buffer standard-output)
  646.         ;; record page position 
  647.         (setq pages-pos-list (cons position pages-pos-list))
  648.         ;; insert page header
  649.         (insert-buffer-substring buffer start end))
  650.       
  651.       (if count-lines-p
  652.           (save-excursion
  653.             (beginning-of-line)
  654.             (insert (format "%3d: " line-count))))
  655.       
  656.       (terpri))
  657.     (forward-line 1)))
  658.  
  659. (defun pages-directory-mode ()
  660.   "Mode for handling the pages-directory buffer.
  661.  
  662. Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go
  663. to the same line in the pages buffer."
  664.  
  665.   (kill-all-local-variables)
  666.   (use-local-map pages-directory-map)
  667.   (setq major-mode 'pages-directory-mode)
  668.   (setq mode-name "Pages-Directory")
  669.   (make-local-variable 'pages-buffer)
  670.   (make-local-variable 'pages-pos-list)
  671.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  672.  
  673. (defun pages-directory-goto ()
  674.   "Go to the corresponding line in the pages buffer."
  675.  
  676. ;;; This function is mostly a copy of `occur-mode-goto-occurrence'
  677.  
  678.   (interactive)
  679.   (if (or (not pages-buffer)
  680.       (not (buffer-name pages-buffer)))
  681.       (progn
  682.     (setq pages-buffer nil
  683.           pages-pos-list nil)
  684.     (error "Buffer in which pages were found is deleted.")))
  685.   (beginning-of-line)
  686.   (let* ((pages-number (1- (count-lines (point-min) (point))))
  687.      (pos (nth pages-number pages-pos-list))
  688.          (end-of-directory-p (eobp))
  689.          (narrowing-p  pages-directory-buffer-narrowing-p))
  690.     (pop-to-buffer pages-buffer)
  691.     (widen)
  692.     (if end-of-directory-p
  693.         (goto-char (point-max))
  694.       (goto-char (marker-position pos)))
  695.     (if narrowing-p (narrow-to-page))))
  696.  
  697.  
  698. ;;;; The `pages-directory-for-addresses' function and ancillary code
  699.  
  700. (defvar pages-addresses-file-name "~/addresses"
  701.   "*Standard name for file of addresses.  Entries separated by `page-delimiter'.
  702. Used by `pages-directory-for-addresses' function.")
  703.  
  704. (defvar pages-directory-for-addresses-narrowing-p t
  705.   "*If non-nil, `pages-directory-goto' narrows addresses buffer to entry.")
  706.  
  707. (defun pages-directory-for-addresses (&optional filename)
  708.   "Find addresses file and display its directory.
  709. By default, create and display directory of `pages-addresses-file-name'.
  710. Optional argument is FILENAME.  In interactive use, with prefix
  711. argument, prompt for file name and provide completion.
  712.  
  713. Move point to one of the lines in the displayed directory,
  714. then use C-c C-c to go to the same line in the addresses buffer."
  715.  
  716.   (interactive
  717.    (list (if current-prefix-arg
  718.              (read-file-name "Filename: " pages-addresses-file-name))))
  719.  
  720.   (if (interactive-p)
  721.       (message "Creating directory for: %s "
  722.                (or filename pages-addresses-file-name)))
  723.   (if (file-exists-p (or filename pages-addresses-file-name))
  724.       (progn
  725.         (set-buffer
  726.          (find-file-noselect
  727.           (expand-file-name
  728.            (or filename pages-addresses-file-name))))
  729.         (widen)
  730.         (pages-directory t nil nil)
  731.         (pages-directory-address-mode)
  732.         (setq pages-directory-buffer-narrowing-p 
  733.               pages-directory-for-addresses-narrowing-p)
  734.         (delete-other-windows)
  735.         (save-excursion
  736.           (goto-char (point-min))
  737.           (delete-region (point) (save-excursion (end-of-line) (point)))
  738.           (insert
  739.            "=== Address List Directory: use `C-c C-c' to go to page under cursor. ===")
  740.           (set-buffer-modified-p nil)
  741.           ))
  742.     (error "No addresses file found!")))
  743.  
  744. (defun pages-directory-address-mode ()
  745.   "Mode for handling the Addresses Directory buffer.
  746.  
  747. Move point to one of the lines in this buffer, then use C-c C-c to go
  748. to the same line in the pages buffer."
  749.  
  750.   (use-local-map pages-directory-map)
  751.   (setq major-mode 'pages-directory-address-mode)
  752.   (setq mode-name "Addresses Directory")
  753.   (make-local-variable 'pages-buffer)
  754.   (make-local-variable 'pages-pos-list)
  755.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  756.  
  757. ;;; page-ext.el ends here
  758.  
  759.